#!/bin/bash

# Downloads artifacts that are deployed to TripleA servers
# Places artifacts in ansible folder structure where ansible can then find those files.

set -eu

if [ "$#" -lt 1 ]; then
  echo "Usage: $0 [Version]"
  exit 1
fi

VERSION=${1-}

DOWNLOAD_URL="https://github.com/triplea-game/triplea/releases/download/$VERSION"

function downloadZippedJarFile() {
  local -r fileName="$1"
  local -r installPath="$2"
  local -r tempDir=$(mktemp -d)

  wget -P "$tempDir" "$DOWNLOAD_URL/$fileName.zip"
  unzip "$tempDir/$fileName.zip" -d "$tempDir/"
  mkdir -p "$installPath"
  mv "$tempDir/bin/$fileName.jar" "$installPath/"
  rm -rf "$tempDir"
}

downloadZippedJarFile "triplea-game-headless-$VERSION" "ansible/roles/bot/files"
downloadZippedJarFile "triplea-http-server-$VERSION" "ansible/roles/http_server/files"

wget "$DOWNLOAD_URL/migrations.zip"
mkdir -p ansible/roles/flyway/files/
mv migrations.zip ansible/roles/flyway/files/
